<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class PorhomeController extends Controller
{
   public function index()
    {
	   	$uid = session('uid');
	   	$list = DB::table('users')->where('id',$uid)->first();
        // dd($list);
	    return view('home.order.porhome',['list'=>$list]);
	
    }

    public function update($id,Request $request)
    {
        $data = $request->only('username','phone','email','pic');
         if ($request->hasFile('pic')) {
            if ($request->file('pic')->isValid()) {
                $file = $request->file('pic');
                $ext = $file->getClientOriginalExtension();
                $pic = time().rand(1000,9999).'.'.$ext;
                $file->move('./uploads/',$pic);
                $data['pic'] = $pic;
                if($file->getError()>0){
                    echo '上传失败';
                }else{
                    echo '上传成功';
                }
            }
        }else{
            echo "<script>alert('请选择图片')</script>";
            return redirect('order/porhome');
        }
        $row = DB::table('users')->where('id',$id)->update($data);
        if($row>0){
            echo '<script>alert("修改成功");window.location.href="/portal";</script>';
        }else{
            echo '<script>alert("修改失败");window.location.href="/order/porhome";</script>';
        }
    }
    
}
